<?php

namespace App\Http\Model\CoolNumberShoppingMall;

use App\Exceptions\PublicException;
use App\Http\Model\Base;
use App\Http\Model\User;
use Illuminate\Support\Facades\DB;

/**
 * 靓号库
 *
 * Created on 2021/12/6 15:01
 * @Author <yuxiang.zhu@asiainnovations.net>
 */
class CoolNumber extends Base
{
    // is_use
    const IS_USE_YES = 1;//使用中
    const IS_USE_NO = 0;//未使用

    /**
     * 表名字
     *
     * @var string
     */
    protected $table = 'cool_number';

    /**
     * 指示模型是否自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 模型日期列的存储格式。
     *
     * @var string
     */
    protected $dateFormat = 'U';

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = ['id', 'uuid', 'type', 'is_use', 'start_time', 'expiry_time', 'usage_times', 'created_at', 'updated_at'];

    /**
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 靓号类型和正则表达式对应关系
     *
     * Created at 2021/12/6 15:11
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return string[]
     */
    public function typeAndRules(): array
    {
        return [
            '*AAA*' => '/^\d*?(\d)\\1\\1\d*?$/',
            '*[66|77|88|99]' => '/^\d*?(66|77|88|99)$/',
            '*AABB*' => '/^\d*?(\d)\\1(?!\\1)(\d)\\2\d*?$/',
            '*ABAB*' => '/^\d*((\d)(?!\\1)(\d))\\1\d*$/',
            '*ABC*' => '/^\d*?(123|234|345|456|567|678|789|987|876|765|654|543|432|321)\d*?$/',
            '*ABCABC*' => '/^\d*?((\d)(\d)(\d))\\1\d*?$/',
            '*ABCDABCD*' => '/^\d*?((\d)(\d)(\d)(\d))\\1\d*?$/',
            '*[1314|520|521|168|518|918]*' => '/^\d*?(1314|520|521|168|518|918)\d*?$/',
            '四位生日未号' => '/^\d*?((0[1,2,3,4,5,6,7,8,9]|10|11|12)([0,1,2]\d|30|31|32))$/',
            '年开头' => '/^(20[0,1,2,3]\d)\d*?$/',
            '年结尾' => '/^\d*?(20[0,1,2,3]\d)$/',
        ];
    }

    /**
     * 获取靓号类型
     *
     * Created at 2021/12/6 15:12
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return array
     */
    public function getTypes(): array
    {
        return array_keys($this->typeAndRules());
    }

    /**
     * 获取靓号类型
     *
     * Created at 2021/12/6 15:16
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param string $subject
     * @return string
     */
    public function getCoolNumberType(string $subject): string
    {
        $type = '';
        $typeAndRules = $this->typeAndRules();
        foreach ($typeAndRules as $typeName => $rule) {
            if (preg_match($rule, $subject)) {
                $type = $typeName;
                break;
            }
        }

        return $type;
    }

    /**
     * 添加靓号
     *
     * Created at 2021/12/6 17:10
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param string $subject
     * @throws PublicException
     */
    public function add(string $subject)
    {
        $subject = trim($subject);
        $type = $this->getCoolNumberType($subject);
        if (!$type) {
            throw new PublicException('要添加的数字不是靓号');
        }

        $query = self::query()->whereRaw('uuid="' . $subject . '"')->count();
        if ($query) {//记录已经存在了
            throw new PublicException('靓号已存在');
        }

        $query = User::query()->select(['user_id'])->whereRaw('uuid="' . $subject . '"')->count();
        if ($query) {
            throw new PublicException('靓号已被使用');
        }

        self::query()->create(['uuid' => $subject, 'type' => $type, 'is_use' => self::IS_USE_NO,]);
    }

    /**
     * 更新靓号使用次数
     *
     * Created at 2021/12/8 16:29
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param string $uuid
     */
    public function incUsageTimes(string $uuid)
    {
        self::query()->whereRaw('uuid="' . $uuid . '"')->increment('usage_times');
    }
}